#!/usr/bin/env bash

if
  [[ "$rvm_trace_flag" -eq 2 ]]
then
  set -x
  export rvm_trace_flag
fi

source "$rvm_scripts_path/base"
source "$rvm_scripts_path/functions/autolibs"
source "$rvm_scripts_path/functions/detect/system"
source "$rvm_scripts_path/functions/version"
source "$rvm_scripts_path/functions/version-installed-when"

version_for()
{
  \typeset binary
  binary=${1:-""}
  if
    builtin command -v "$binary" >/dev/null
  then
    $binary --version | \command \head -n1
  else
    echo "not installed"
  fi
}

info_system()
{
  source "$rvm_scripts_path/functions/detect/xcode"

  rvm_info="
  system:
    uname:        \"${_system_info}\"
    name:         \"${_system_name}\"
    version:      \"${_system_version}\"
    architecture: \"${_system_arch}\"
    bash:         \"$(command -v bash) => $(version_for bash)\"
    zsh:          \"$(command -v zsh) => $(version_for zsh)\"
    remote_path:  \"$(__rvm_system_path - 99 )\""

  [[ "$_system_name" == "OSX" ]] && rvm_info="${rvm_info}
    xcode:        \"$(__rvm_detect_xcode_version)\""

  rvm_info="${rvm_info}\n"
}

info_rvm()
{
  __rvm_autolibs_get
  __rvm_autolibs_translate_description

  rvm_info="
  rvm:
    version:      \"${rvm_version}\"
    updated:      \"$(__rvm_version_installed_when)\"
    path:         \"$rvm_path\"
    autolibs:     \"[${rvm_autolibs_flag}] ${rvm_autolibs_flag_description}\"
"
}

info_ruby()
{
  [[ "$(__rvm_env_string)" == "system" ]] && return
  ruby=$(builtin command -v ruby)
  if
    [[ $? -eq 0 ]] && [[ -x "$ruby" ]]
  then
    full_version="$($ruby -v)"
  fi
  rvm_info="
  ruby:
    interpreter:  \"$(printf "%b" "${full_version}" | __rvm_awk '{print $1}')\"
    version:      \"$(printf "%b" "${full_version}" | __rvm_awk '{print $2}')\"
    date:         \"$(printf "%b" "${full_version}" | __rvm_sed 's/^.*(\([0-9]\{4\}\(-[0-9][0-9]\)\{2\}\).*$/\1/')\"
    platform:     \"$(printf "%b" "${full_version}" | __rvm_sed 's/^.*\[//' | __rvm_sed 's/\].*$//')\"
    patchlevel:   \"$(printf "%b" "${full_version}" | __rvm_sed 's/^.*(//' | __rvm_sed 's/).*$//')\"
    full_version: \"${full_version}\"
"
}

info_homes()
{
  rvm_info="
  homes:
    gem:          \"${GEM_HOME:-"not set"}\"
    ruby:         \"${MY_RUBY_HOME:-"not set"}\"
"
}

info_binaries()
{
  rvm_info="
  binaries:
    ruby:         \"$(command -v ruby)\"
    irb:          \"$(command -v irb)\"
    gem:          \"$(command -v gem)\"
    rake:         \"$(command -v rake)\"
"
}

info_environment()
{
  rvm_info="
  environment:
    PATH:         \"${PATH:-""}\"
    GEM_HOME:     \"${GEM_HOME:-""}\"
    GEM_PATH:     \"${GEM_PATH:-""}\"
    MY_RUBY_HOME: \"${MY_RUBY_HOME:-""}\"
    IRBRC:        \"${IRBRC:-""}\"
    RUBYOPT:      \"${RUBYOPT:-""}\"
    gemset:       \"$(__rvm_current_gemset)\"\n
"
  if [[ -n "${MAGLEV_HOME:-""}" ]]
  then rvm_info="$rvm_info\n  MAGLEV_HOME: \"$MAGLEV_HOME\""
  fi
  rvm_info="$rvm_info\n"
}

info_debug()
{
  rvm_info="

$(__rvm_print_headline)
  $("$rvm_scripts_path/info" "$rvm_ruby_string" "" )
  PATH:\n$(printf "%b" "$PATH" | __rvm_awk -F":" '{print $1":"$2":"$3":"$4":"$5}' )
  uname -a: ${_system_info}
  permissions: $(__rvm_ls -la "$rvm_path" "$rvm_rubies_path")
"

  if [[ "Darwin" == "${_system_type}" ]]
  then
    rvm_info="$rvm_info
  sw_vers: $(sw_vers | \command \tr "\n" ',')
  ARCHFLAGS: ${ARCHFLAGS:-""}
  LDFLAGS: ${LDFLAGS:-""}
  CFLAGS: ${CFLAGS:-""}
"
  fi
  for file_name in "$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.zshenv"
  do
    if
      [[ -s "$file_name" ]]
    then
      rvm_info="$rvm_info\n$file_name:\n$(__rvm_grep 'rvm' "$file_name" 2>/dev/null || true)"
    fi
  done

  debug_files=(
    "$rvm_path/config/alias" "$rvm_path/config/system" "$rvm_path/config/db"
    "/etc/rvmrc" "$HOME/.rvmrc" "/etc/gemrc" "$HOME/.gemrc"
  )

  for file_name in "${debug_files[@]}"
  do
    if
      [[ -f "$file_name" && -s "$file_name" ]]
    then
      rvm_info="$rvm_info\n$file_name \(filtered\):\n$(__rvm_awk '!/assword|_key/{print}' "$file_name" )\n"
    fi
  done
  rvm_info="$rvm_info\ngem sources:\n$(gem sources | __rvm_awk '/gems/{print}')\n\n"
}

info_sections()
{
  for section in $(printf "%b" "${sections//,/ }")
  do
    rvm_info=""
    "info_${section}"
    printf "%b" "$rvm_info"
  done
}

rvm_ruby_gem_home="${rvm_ruby_gem_home:-${GEM_HOME:-""}}"

if
  [[ ! -d "$rvm_ruby_gem_home" ]] &&
  builtin command -v gem > /dev/null 2>&1
then
  rvm_ruby_gem_home="$(gem env home)"
fi

rvm_info=""
args=($*)

ruby_strings="${args[$__array_start]// /}"
args[$__array_start]=""
args=(${args[@]})

sections="${args// /}"
all_sections="system rvm ruby homes binaries environment"

# TODO: Figure out what was the thought here and remove external match script
#       dependency
if
  __rvm_string_match "$all_sections debug" "*${ruby_strings%%,*}*"
then
  sections="$ruby_strings"
  ruby_strings=""
fi

if
  [[ -z "${sections// /}" ]]
then
  sections="$all_sections"
fi

if
  [[ -z "$ruby_strings" ]]
then
  printf "%b" "\n$(__rvm_env_string):\n"
  info_sections
else
  for ruby_string in ${ruby_strings//,/ }
  do
    __rvm_become "$ruby_string"
    printf "%b" "\n$(__rvm_env_string):\n"
    info_sections
  done
fi
